package com.fengye.middleware.rabbitmq.boot.messageType.topic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengye.middleware.rabbitmq.boot.messageType.EventInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 *
 * @author sky
 * @date 2021年06月09日 11:22 上午
 */
@Component
@Slf4j
public class TopicConsumer {

//    @RabbitListener(queues = {"${mq.topic.queue.one.name}"}, containerFactory = "singleListenerContainer")
//    public void consumerMsgOne(@Payload byte[] msg) {
//        try {
//            String message = new String(msg, StandardCharsets.UTF_8);
//            log.info("TopicExchange接收消息(*)：{}", message);
//        } catch (Exception e) {
//            log.error("TopicExchange接收消息异常：", e.fillInStackTrace());
//        }
//    }

    @RabbitListener(queues = {"${mq.topic.queue.one.name}"}, containerFactory = "stringListenerContainerFactory")
    public void consumerMsgOne(Message message) {
        try {
            byte[] body = message.getBody();
            log.info("TopicExchange接收消息(*)：{}", new String(body, StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("TopicExchange接收消息异常：", e.fillInStackTrace());
        }
    }

    @RabbitListener(queues = {"${mq.topic.queue.two.name}"}, containerFactory = "stringListenerContainerFactory")
    public void consumerMsgTwo(Message message) {
        try {
            byte[] body = message.getBody();
            log.info("TopicExchange接收消息(#)：{}", new String(body, StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("TopicExchange接收消息异常：", e.fillInStackTrace());
        }
    }

//    @RabbitListener(queues = {"${mq.topic.queue.two.name}"}, containerFactory = "singleListenerContainer")
//    public void consumerMsgTwo(@Payload byte[] msg) {
//        try {
//            String message = new String(msg, StandardCharsets.UTF_8);
//            log.info("TopicExchange接收消息(#)：{}", message);
//        } catch (Exception e) {
//            log.error("TopicExchange接收消息异常：", e.fillInStackTrace());
//        }
//    }
}
